﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoTestSystem
{
    public partial class Edit : Form
    {
        public Edit()
        {
            InitializeComponent();
            this.MaximizeBox = false;

            uiPanel1.Dock = DockStyle.Fill; uiPanel3.Dock = DockStyle.Fill;
            uiPanel3.Visible = false;

            uiButton1.Click += uiButton1_Click;

            DataGridViewButtonColumn btnEnable = new DataGridViewButtonColumn();
            //btnEnable.HeaderText = "操作";
            btnEnable.Name = "1";
            //btnEnable.Text = "ENABLE";
            btnEnable.UseColumnTextForButtonValue = true;
            btnEnable.FillWeight = 1;
            uiDataGridView1.Columns.Add(btnEnable);

         


            if (Form1.stepList.Count > 0)
            {
                foreach (StepClass item in Form1.stepList)
                {
                    if (item.IsEnable == "✔")
                    {
                        Form1.stepList.RemoveAt(Form1.stepList.IndexOf(item));
                        Form1.stepList.Insert(0, item);
                        break;
                    }
                }


                uiDataGridView1.DataSource = null;
                uiDataGridView1.DataSource = Form1.stepList;


                uiDataGridView1.Columns[0].FillWeight = 0.1f;
             
                uiDataGridView1.Columns[1].FillWeight = 100;
                //uiDataGridView1.Columns[2].FillWeight = 180;
                uiDataGridView1.Columns[2].FillWeight = 80;
                uiDataGridView1.Columns[3].FillWeight = 60;
                uiDataGridView1.Columns[4].FillWeight = 60;

            }

             
            for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
            {
                uiDataGridView1.Rows[i].Cells[3].Value = "查询";
            }


        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {  if (e.ColumnIndex == 4 && e.RowIndex >= 0)
                {
                    string tempSt = uiDataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    if (!string.IsNullOrEmpty(tempSt))
                    {
                        string filePath = Path.Combine(Application.StartupPath + @"\AreaImage", tempSt + ".png");
                        if (File.Exists(filePath))
                        {
                            Process.Start(filePath);
                        }
                        else
                        {
                            MessageBox.Show("文件 " + tempSt + " 不存在。");
                        }
                    }
                }

            }
            catch { }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            StepForm sf = new StepForm(this, true, "");
            //sf.Form3ParametersCaptured += Form3_Form3ParametersCaptured;
            sf.ShowDialog();


            //StepControl sc = new StepControl(this, true, "");
            ////sc.Dock = DockStyle.Fill;
            //uiPanel3.Controls.Add(sc);


            //uiPanel1.Visible = false;
            //uiPanel3.Visible = true;
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            //    if (uiButton2.Text == "Delete")
            //    {
            //        uiDataGridView1.Columns[0].Visible = true;
            //        uiButton2.Text = "Cancel";
            //    }
            //    else
            //    {
            //        uiDataGridView1.Columns[0].Visible = false;
            //        uiButton2.Text = "Delete";
            //    }
            //}
        }

        int selectRowIndex = 0;
        private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        //取消当前选中行的选中状态
                        uiDataGridView1.ClearSelection();
                        //右键单击的行设定为选中状态
                        uiDataGridView1.Rows[e.RowIndex].Selected = true;
                        //将右键单击的单元格设定为当前单元格
                        uiDataGridView1.CurrentCell = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        uiContextMenuStrip1.Show(MousePosition.X, MousePosition.Y);

                        selectRowIndex = e.RowIndex;
                    }
                }
            }
            catch (Exception)
            {
                //MessageBox.Show("请选择内容");
            }
        }

        private void SetForm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
            {
                if (uiDataGridView1.Rows[i].Cells[4].Value.ToString() == "✔")
                {
                    uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                    uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White;
                    uiDataGridView1.Rows[i].Cells[3].Style.ForeColor = Color.Blue;
                }
                else
                {
                    uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
                    uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                    uiDataGridView1.Rows[i].Cells[3].Style.ForeColor = Color.Blue;
                }

            }
        }

        private void uiPanel1_Click(object sender, EventArgs e)
        {

        }

        private void enableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
                string tempSt = uiDataGridView1.Rows[selectRowIndex].Cells[1].Value.ToString();

                foreach (StepClass item in Form1.stepList)
                {
                    if (item.StepName.ToString() == tempSt)
                    {
                        item.IsEnable = "✔";


                        Form1.SelectedKind = "机种：" + item.StepName + "      步骤：" + item.StepDetail + "      文字：" + item.CompareStr;

                        ToolClass.UpdateLocData(item);
                    }
                    else
                    {
                        if (item.IsEnable == "✔")
                        {
                            item.IsEnable = "✘";
                            ToolClass.UpdateLocData(item);
                        }



                    }

                }
            //foreach (StepClass item in Form1.stepList)
            //{
            //    if (item.IsEnable == "✔")
            //    {
            //        Form1.stepList.RemoveAt(Form1.stepList.IndexOf(item));
            //        Form1.stepList.Insert(0, item);
            //        break;
            //    }
            //}

            uiDataGridView1.DataSource = null;
                uiDataGridView1.DataSource = Form1.stepList;

                Form1.AddStepData = true;
            uiDataGridView1.Columns[0].FillWeight = 0.1f;

            uiDataGridView1.Columns[1].FillWeight = 100;
            //uiDataGridView1.Columns[2].FillWeight = 180;
            uiDataGridView1.Columns[2].FillWeight = 80;
            uiDataGridView1.Columns[3].FillWeight = 60;
            uiDataGridView1.Columns[4].FillWeight = 60;

            uiDataGridView1.Rows[selectRowIndex].DefaultCellStyle.BackColor = Color.Green;
                uiDataGridView1.Rows[selectRowIndex].DefaultCellStyle.ForeColor = Color.White;
                uiDataGridView1.Columns[4].DefaultCellStyle.ForeColor = Color.Blue;


            //for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
            //{
            //    if (uiDataGridView1.Rows[i].Cells[5].Value.ToString() == "✔")
            //    {
            //        uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
            //        uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White;
            //        uiDataGridView1.Rows[i].Cells[4].Style.ForeColor = Color.Blue;
            //    }
            //    else
            //    {
            //        uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
            //        uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
            //        uiDataGridView1.Rows[i].Cells[4].Style.ForeColor = Color.Blue;
            //    }
            //}

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string tempSt = uiDataGridView1.Rows[selectRowIndex].Cells[1].Value.ToString();

            StepForm sf = new StepForm(this, false, tempSt);
            sf.ShowDialog();
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string tempSt = uiDataGridView1.Rows[selectRowIndex].Cells[1].Value.ToString();


            foreach (StepClass item in Form1.stepList)
            {
                if (item.StepName.ToString() == tempSt)
                {
                    ToolClass.RemoveSingleLocData(item);
                    Form1.stepList.Remove(item);

                    break;
                }
            }

            //foreach (StepClass item in Form1.stepList)
            //{
            //    if (item.IsEnable == "✔")
            //    {
            //        Form1.stepList.RemoveAt(Form1.stepList.IndexOf(item));
            //        Form1.stepList.Insert(0, item);
            //        break;
            //    }
            //}

            uiDataGridView1.DataSource = null;
            uiDataGridView1.DataSource = Form1.stepList;

            Form1.AddStepData = true;
            uiDataGridView1.Columns[0].FillWeight = 0.1f;

            uiDataGridView1.Columns[1].FillWeight = 100;
            //uiDataGridView1.Columns[2].FillWeight = 180;
            uiDataGridView1.Columns[2].FillWeight = 80;
            uiDataGridView1.Columns[3].FillWeight = 60;
            uiDataGridView1.Columns[4].FillWeight = 60;
            uiDataGridView1.Columns[3].DefaultCellStyle.ForeColor = Color.Blue;

            for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
            {
                if (uiDataGridView1.Rows[i].Cells[4].Value.ToString() == "✔")
                {
                    uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                    uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White;
                    uiDataGridView1.Rows[i].Cells[3].Style.ForeColor = Color.Blue;
                }
                else
                {
                    uiDataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
                    uiDataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                    uiDataGridView1.Rows[i].Cells[3].Style.ForeColor = Color.Blue;
                }
            }
        }

        //private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        //{

        //        if (e.Button == MouseButtons.Right)
        //        {
        //            if (e.RowIndex >= 0)
        //            {
        //                uiDataGridView1.ClearSelection();
        //            uiDataGridView1.Rows[e.RowIndex].Selected = true;
        //            uiDataGridView1.CurrentCell = uiDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        //                uiContextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
        //            }
        //        }


        //}
    }
}
